home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX 6.2 Applications 1996 May
/
SGI IRIX 6.2 Applications 1996 May.iso
/
dist
/
impr_dev.idb
/
usr
/
impressario
/
src
/
examples
/
libprintui
/
printdialog.c.z
/
printdialog.c
Wrap
C/C++ Source or Header
|
1996-05-06
|
15KB
|
548 lines
/**************************************************************************
* *
* Copyright (c) 1991 Silicon Graphics, Inc. *
* All Rights Reserved *
* *
* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI *
* *
* The copyright notice above does not evidence any actual of intended *
* publication of such source code, and is an unpublished work by Silicon *
* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is *
* the property of Silicon Graphics, Inc. Any use, duplication or *
* disclosure not specifically authorized by Silicon Graphics is strictly *
* prohibited. *
* *
* RESTRICTED RIGHTS LEGEND: *
* *
* Use, duplication or disclosure by the Government is subject to *
* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in *
* Technical Data and Computer Software clause at DFARS 52.227-7013, *
* and/or in similar or successor clauses in the FAR, DOD or NASA FAR *
* Supplement. Unpublished - rights reserved under the Copyright Laws of *
* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N. *
* Shoreline Blvd., Mountain View, CA 94039-7311 *
**************************************************************************
*
* File: printdialog.c
*
* Description: Presents a push button that brings up a PrintBox dialog
* box when pressed.
*
**************************************************************************/
#ident "$Revision: 1.5 $"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <Xm/Xm.h>
#include <X11/StringDefs.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
#include <Xm/MessageB.h>
#include <Sgm/PrintBox.h>
/* Program globals */
static char *prog_name; /* Program instance name (no path) */
static char *prog_class = "Printdialog"; /* Program class name */
static Widget top_level; /* Top level widget */
static Widget print_box; /* printBox widget */
static Widget error_dialog; /* Error dialog */
static XtAppContext app_context; /* Application context */
/* Replacement error messages */
static char *no_spooler_mess[] = {
"The print scheduler /usr/lib/lpsched is not running.",
"Contact your system administrator."
};
static int no_spooler_mess_lines = sizeof(no_spooler_mess) / sizeof(char*);
/* Help message */
static char *help_mess[] = {
"'printdialog' simply instantiates a PrintBox dialog box."
};
static int help_mess_lines = sizeof(help_mess) / sizeof(char*);
/* Local functions */
static void popup_cb(Widget, XtPointer, XtPointer);
static void exit_cb(Widget, XtPointer, XtPointer);
static void cancel_cb(Widget, XtPointer, XtPointer);
static void print_cb(Widget, XtPointer, XtPointer);
static void error_cb(Widget, XtPointer, XtPointer);
static void help_cb(Widget, XtPointer, XtPointer);
static Widget create_error_dialog(void);
static XmString create_message(char**, int);
static void manage_error_cb(Widget, XtPointer, XEvent*, Boolean*);
/**************************************************************************
*
* Function: main
*
* Description: Program entry point
*
* Parameters:
* argc (I) - command line argument count
* argv (I) - command line arguments
*
* Return: None
*
**************************************************************************/
int main(int argc, char *argv[])
{
Widget rc, popup_button, exit_button;
char *str;
/*
* Set program instance name
*/
str = strrchr(*argv, '/');
prog_name = strdup((str == NULL) ? argv[0]: str + 1);
/*
* Initialize the X app connection.
*/
top_level = XtAppInitialize(&app_context, prog_class,
NULL, 0, &argc, argv, NULL, NULL, 0);
/*
* Create the top level manager widget
*/
rc = XtCreateManagedWidget("buttonRC", xmRowColumnWidgetClass,
top_level, NULL, 0);
/*
* Create the popup push button
*/
popup_button = XtCreateManagedWidget("popupButton",
xmPushButtonWidgetClass, rc, NULL, 0);
/*
* Create the exit push button
*/
exit_button = XtCreateManagedWidget("exitButton",
xmPushButtonWidgetClass, rc, NULL, 0);
/*
* Add callbacks
*/
XtAddCallback(exit_button, XmNactivateCallback, exit_cb, NULL);
XtAddCallback(popup_button, XmNactivateCallback, popup_cb, NULL);
/*
* Realize all widgets
*/
XtRealizeWidget(top_level);
/*
* Start processing events.
*/
XtAppMainLoop(app_context);
/* NOTREACHED */
return(1);
}
/*=========================================================================
LOCAL FUNCTIONS
=========================================================================*/
/**************************************************************************
*
* Function: popup_cb
*
* Description: Called when the push button is pressed. The function
* simply manages the PrintBox dialog box thereby poping it up.
*
* Parameters:
* w (I) - invoking widget
* client_data (I) - client data
* call_data (I) - standard callback info
*
* Return: none
*
**************************************************************************/
static void popup_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
/*
* If the PrintBox dialog has not been created, create it
*/
if (!print_box) {
Arg wargs[5];
register int n = 0;
/*
* Create the PrintBox dialog.
*/
XtSetArg(wargs[n], XmNautoUnmanage, False); n++;
print_box = PuiCreatePrintDialog(top_level, "printDialog", wargs, n);
/*
* Add callbacks
*/
XtAddCallback(print_box, PuiNcancelCallback, cancel_cb, NULL);
XtAddCallback(print_box, PuiNhelpCallback, help_cb, NULL);
XtAddCallback(print_box, PuiNjobInfoCallback, print_cb, NULL);
XtAddCallback(print_box, PuiNerrorCallback, error_cb, NULL);
XtAddCallback(print_box, PuiNoptionErrorCallback, error_cb, NULL);
}
/*
* Pop up the dialog
*/
if (!XtIsManaged(print_box))
XtManageChild(print_box);
}
/**************************************************************************
*
* Function: exit_cb
*
* Description: Causes the program to exit.
*
* Parameters:
* w (I) - invoking widget
* client_data (I) - client data
* call_data (I) - standard callback info
*
* Return: none
*
**************************************************************************/
static void exit_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
exit(0);
}
/**************************************************************************
*
* Function: cancel_cb
*
* Description: Cancel button popdown routine.
*
* Parameters:
* w (I) - invoking widget
* client_data (I) - client data
* call_data (I) - standard callback info
*
* Return: none
*
**************************************************************************/
/* ARGSUSED */
static void cancel_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
if (XtIsManaged(print_box))
XtUnmanageChild(print_box);
}
/**************************************************************************
*
* Function: print_cb
*
* Description: Print job information callback. This callback is called
* if a print job has been successfully submitted. The job ID is
* printed to standard out and the program terminates.
*
* Parameters:
* w (I) - invoking widget
* client_data (I) - client data
* call_data (I) - standard callback info
*
* Return: none
*
**************************************************************************/
/* ARGSUSED */
static void print_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
PuiPrintBoxCallbackStruct *pb_cb = (PuiPrintBoxCallbackStruct*)call_data;
/*
* Print job ID
*/
(void)printf("request id is %s\n", pb_cb->job_info->job_id);
/*
* Pop down the printbox dialog
*/
if (XtIsManaged(print_box))
XtUnmanageChild(print_box);
}
/**************************************************************************
*
* Function: error_cb
*
* Description: Handles the display of error messages from the printBox
* widget.
*
* Parameters:
* w (I) - invoking widget
* client_data (I) - client data
* call_data (I) - standard callback info
*
* Return: none
*
**************************************************************************/
/* ARGSUSED */
static void error_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
XmString str;
char **err_buf, *estr;
int num_str;
PuiPrintBoxCallbackStruct *pb_cb = (PuiPrintBoxCallbackStruct*)call_data;
Arg arg;
/*
* If first time, create the error dialog window
*/
if (!error_dialog)
error_dialog = create_error_dialog();
/*
* Determine if this is an option panel error or printbox error
*/
if (pb_cb->reason == PuiCR_OPT_ERROR) {
estr = strerror(pb_cb->error_code);
err_buf = &estr;
num_str = 1;
} else {
/*
* Select the appropriate message
*/
switch (pb_cb->error_code) {
case SL_ERR_SPOOLER_ERROR:
SLGetSpoolerError(&err_buf, &num_str);
break;
case SL_ERR_NO_SPOOLERS:
case SL_ERR_SPOOLER_UNKNOWN:
XtAddCallback(error_dialog, XmNokCallback, cancel_cb, NULL);
err_buf = no_spooler_mess;
num_str = no_spooler_mess_lines;
break;
default:
estr = SLErrorString(pb_cb->error_code);
err_buf = &estr;
num_str = 1;
break;
}
}
/*
* Set the message in the error dialog
*/
str = create_message(err_buf, num_str);
XtSetArg(arg, XmNmessageString, str);
XtSetValues(error_dialog, &arg, 1);
XmStringFree(str);
/*
* Bring up the dialog. If the PrintBox dialog is up we simply manage
* the error dialog. Otherwise we watch for it to show up and then
* post the dialog.
*/
if (XtIsRealized(print_box))
XtManageChild(error_dialog);
else
XtAddEventHandler(print_box, VisibilityChangeMask, False,
manage_error_cb, NULL);
}
/**************************************************************************
*
* Function: manage_error_cb
*
* Description: Handles the delayed display of an error dialog. The
* delay was due to the PrintBox dialog not being visible when
* the error occurred. We want it visible so that the error dialog
* does not get hidden behind the PrintBox.
*
* Parameters:
* w (I) - PrintBox widget
* client_data (I) - not used
* event (I) - event that triggered the call
* cont (I) - continue propagating event flag
*
* Return: none
*
**************************************************************************/
static void manage_error_cb(Widget w, XtPointer client_data, XEvent *event,
Boolean *cont)
{
XtRemoveEventHandler(print_box, VisibilityChangeMask, False,
manage_error_cb, NULL);
XtManageChild(error_dialog);
}
/**************************************************************************
*
* Function: help_cb
*
* Description: Displays the help message.
*
* Parameters:
* w (I) - invoking widget
* client_data (I) - not used
* call_data (I) - not used
*
* Return: none
*
**************************************************************************/
/* ARGSUSED */
static void help_cb(Widget w, XtPointer client_data, XtPointer call_data)
{
static Widget help_dialog = NULL;
/*
* If first time, create the help dialog
*/
if (!help_dialog) {
Arg wargs[5];
XmString tstr, mstr;
register int n;
/*
* Create title and message strings
*/
tstr = XmStringCreateSimple("Help");
mstr = create_message(help_mess, help_mess_lines);
/*
* Create a dialog
*/
n = 0;
XtSetArg(wargs[n], XmNdialogTitle, tstr); n++;
XtSetArg(wargs[n], XmNmessageString, mstr); n++;
help_dialog = XmCreateInformationDialog(top_level, "helpDialog",
wargs, n);
/*
* Blow off Help and Cancel
*/
XtUnmanageChild(XmMessageBoxGetChild(help_dialog,
XmDIALOG_CANCEL_BUTTON));
XtUnmanageChild(XmMessageBoxGetChild(help_dialog,
XmDIALOG_HELP_BUTTON));
XmStringFree(tstr);
XmStringFree(mstr);
}
/*
* Display the dialog
*/
XtManageChild(help_dialog);
}
/**************************************************************************
*
* Function: create_error_dialog
*
* Description: Creates the error dialog
*
* Parameters: none
*
* Return: Created dialog widget
*
**************************************************************************/
static Widget create_error_dialog()
{
Widget dialog;
Arg wargs[5];
XmString tstr;
register int n;
/*
* Create some compound strings
*/
tstr = XmStringCreateSimple("Error"); /* Dialog window title */
/*
* Create a modal dialog
*/
n = 0;
XtSetArg(wargs[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
XtSetArg(wargs[n], XmNdialogTitle, tstr); n++;
dialog = XmCreateErrorDialog(print_box, "errorDialog", wargs, n);
/*
* Blow off Help and Cancel
*/
XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
XmStringFree(tstr);
return dialog;
}
/**************************************************************************
*
* Function: create_message
*
* Description: A utility function to create a compound string message from
* a multi-element array of strings.
*
* Parameters:
* str_array (I) - array of message strings.
* num_str (I) - number of strings in array.
*
* Return: A compound string.
*
**************************************************************************/
static XmString create_message(char **str_array, int num_str)
{
XmString xm_newstr, xtemp, xmstr;
register char *str;
register int i;
xmstr = XmStringCreateSimple("");
for (i = 0, str = *str_array; i < num_str; str = *++str_array, i++) {
if (i) {
xm_newstr = XmStringSeparatorCreate();
xtemp = xmstr;
xmstr = XmStringConcat(xtemp, xm_newstr);
XmStringFree(xtemp);
XmStringFree(xm_newstr);
}
xm_newstr = XmStringCreateSimple(str);
xtemp = xmstr;
xmstr = XmStringConcat(xtemp, xm_newstr);
XmStringFree(xtemp);
XmStringFree(xm_newstr);
}
return xmstr;
}